﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Create : MonoBehaviour {
	List<GameObject> listCard= new List<GameObject>();
	List<AudioClip> listAudio= new List<AudioClip>();

	List<GameObject> listNewCard= new List<GameObject>();
	List<AudioClip> listNewAudio= new List<AudioClip>();

	List<GameObject> listTempCard= new List<GameObject>();
	List<AudioClip> listTempAudio= new List<AudioClip>();

	List<GameObject> listMouse =new List<GameObject>();
	List<GameObject> listTempMouse= new List<GameObject>();
	int soThe=2;
	int index;
	int newIndex;
	string nameCard=null;
	string nameAudio=null;
	Vector3 viTriXuatHien;
	bool run= true;
	bool destroy=false;
	bool canTouch=false;

	public GUIStyle loa; 
	// Use this for initialization
	void Start () {
		for(int i=1;i<=9;i++)//load object and audio to list
		{
			listCard.Add((GameObject)Resources.Load(""+i));
			listAudio.Add((AudioClip)Resources.Load("Audio"+i));
		}
		for(int i=0;i<3;i++)
		{
			listMouse.Add((GameObject)Resources.Load("Mouse"));
		}

	}
	
	// Update is called once per frame
	void Update () {
		if (destroy == true) {  //  destroy card when load new card
			for(int i=0;i<listTempCard.Count;i++)
				Destroy(listTempCard[i]);
			destroy=false;run=true;
		}
		if (run == true) 
		{
			run=false;
			for(int i=0;i<soThe;i++)	//load card and audio to temp list 
			{
				index= Random.Range(0,listCard.Count);
				listNewCard.Add((GameObject)listCard[index]);
				listNewAudio.Add((AudioClip)listAudio[index]);
				listCard.RemoveAt(index);
				listAudio.RemoveAt(index);
			}
	
			for(int i=0;i<listNewCard.Count;i++)
			{
				viTriXuatHien=new Vector3(0,1,0);
				if((soThe/2)<=i && soThe>2)
				{
					viTriXuatHien=new Vector3(0,0,0);
				}
			//	Instantiate(listNewCard[i],viTriXuatHien,Quaternion.identity);
				listTempCard.Add((GameObject)Instantiate(listNewCard[i],viTriXuatHien,Quaternion.identity));
			}

			StartCoroutine (WaitTime());


		}

		if(Input.GetMouseButtonDown(0))
		{
			RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
			
			if(hitInfo.collider != null && hitInfo.collider.tag=="Card" && canTouch==true)
			{
				hitInfo.rigidbody.gameObject.GetComponent<AnimationTouch>().PlayAnimation();
				nameCard = "Audio"+hitInfo.rigidbody.gameObject.name;
				Debug.Log(nameAudio+"/"+nameCard);
				if(nameAudio== nameCard)
				{
					soThe++;if(soThe>8)soThe=2;
					destroy=true;nameAudio=null;nameCard=null;
					canTouch=false;
				}

			}
		}
	}


	IEnumerator WaitTime()
	{
		for(int i=0;i<listMouse.Count;i++)
		{
			listTempMouse.Add((GameObject)Instantiate(listMouse[i],new Vector3(0,-4,-1),Quaternion.identity));

		}
		listTempMouse.Add((GameObject)Instantiate(Resources.Load("FadeBackGround"),new Vector3(0,0,-1),Quaternion.identity));
		for(int i=0;i<listTempMouse.Count-1;i++)
		{
			yield return new WaitForSeconds(1);
			listTempMouse[i].GetComponent<AnimationTouch>().PlayAnimation();
		}
		yield return new WaitForSeconds (1.3f);
		for(int i=0;i<listTempMouse.Count;i++)
		{
			Destroy(listTempMouse[i]);
		}
		listTempMouse.Clear();
		
		newIndex = Random.Range (0,listNewAudio.Count);
		listTempAudio.Add ((AudioClip)listNewAudio[newIndex]);
		audio.PlayOneShot ((AudioClip)listNewAudio[newIndex]);
		nameAudio = listNewAudio [newIndex].name+"(Clone)";
		
		for(int i=0;i<listNewCard.Count;i++)
		{
			listCard.Add((GameObject)listNewCard[i]);
			listAudio.Add((AudioClip)listNewAudio[i]);
		}
		listNewCard.Clear();
		listNewAudio.Clear();
		canTouch = true;
	}

	void OnGUI()
	{
		//if (GUI.RepeatButton (new Rect(Screen.width*4/5,10,40,40),"",loa)) 
		//{
		//	audio.PlayOneShot ((AudioClip)listTempAudio[listTempAudio.Count-1]);

		//}
	}
}
